//
//  PreviewFileVC.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/8/25.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "PreviewFileVC.h"

@implementation PreviewFileItem

@end

@interface PreviewFileVC ()< QLPreviewControllerDelegate, QLPreviewControllerDataSource>

@property (nonatomic,copy) NSString *fileUrl;
@property (nonatomic,copy) NSString *fileName;

@property (nonatomic,assign) BOOL isLocal;

@end



@implementation PreviewFileVC


//+ (instancetype) previewFileWithNetUrl:(NSString *)fileUrl fileName:(NSString *)fileName{
//    PreviewFileVC  *previewVC = [[PreviewFileVC alloc]init];
//    previewVC.fileUrl = fileUrl;
//    previewVC.fileName =  fileName;
//    previewVC.isLocal = NO;
//    return previewVC;
//}

+ (instancetype) previewFileWithLocalStorageUrl:(NSString *)fileUrl fileName:(NSString *)fileName{
    PreviewFileVC  *previewVC = [[PreviewFileVC alloc]init];
    previewVC.fileUrl = fileUrl;
    previewVC.fileName =  fileName;
    previewVC.isLocal = YES;
    return previewVC;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if(!IsEmptyStr(self.fileUrl)){
        self.dataSource = self;
        self.delegate = self;
    }
}

- (BOOL)isCanPreviewItem{
    NSURL *url;
    if(self.isLocal){
        url = [NSURL fileURLWithPath:self.fileUrl];
    }else{
        url = [NSURL URLWithString:self.fileUrl];
    }
    return [QLPreviewController canPreviewItem:(id<QLPreviewItem>)url];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

- (id<QLPreviewItem>)previewController:(QLPreviewController*)controller previewItemAtIndex:(NSInteger)index
{
    PreviewFileItem * previewItem = [[PreviewFileItem alloc]init];
    previewItem.previewItemTitle = self.fileName;
    NSURL *URL = nil;
    if(self.isLocal){
        URL = [NSURL fileURLWithPath:self.fileUrl];
    }else{
       URL = [NSURL URLWithString:self.fileUrl];
    }
    previewItem.previewItemURL = URL ;
    return previewItem;
}



@end
